package zb.blog.controller;

import com.github.pagehelper.PageInfo;
import com.qiniu.common.QiniuException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import zb.blog.base.bean.Result;
import zb.blog.base.global.GlobalVar;
import zb.blog.bean.ArticleBean;
import zb.blog.service.IArticleService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: BinZhang
 * @Date: 2018/5/1 15:23
 * @Description:文章相关
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService iArticleService;

    /**
     * @Author: BinZhang
     * @Date: 2018/5/11 22:23
     * @Description:用于显示前台翻牌
     */
    @RequestMapping("/getArticleByStatus")
    @ResponseBody
    public PageInfo<ArticleBean> getArticleByStatus(int pageNum, int pageSize, Integer navBarId, String keyWord) {
        Map<String, Object> param = new HashMap<>();
        param.put("navBarId", navBarId);
        param.put("keyWord", keyWord);
        param.put("status", 1);
        param.put("type", 3);
        return iArticleService.getArticleByStatus(pageNum, pageSize, param);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/11 22:24
     * @Description:后台文章查筛选维护
     */
    @RequestMapping("/back/getArticleAll")
    @ResponseBody
    public PageInfo<ArticleBean> getArticleAll(@RequestBody Map<String, String> param) {
        return iArticleService.getArticleAll(param);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/12 9:52
     * @Description:添加新的文章
     */
    @RequestMapping("/back/addArticle")
    @ResponseBody
    public Result addArticle(ArticleBean articleBean, HttpSession session) {
        articleBean.setType(3);//3 表示文章的类型
        return iArticleService.addArticle(articleBean, session);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/12 10:55
     * @Description:根据id获取文章信息
     */
    @RequestMapping("/getArticleById")
    @ResponseBody
    public Result getArticleById(String id) {
        return iArticleService.getArticleById(id);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/13 21:23
     * @Description:根据id更新文章的状态
     */
    @RequestMapping("/back/updateArticleStatus")
    @ResponseBody
    public Result updateArticleStatus(String id, Integer status) {
        return iArticleService.updateArticleStatus(id, status);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/14 14:52
     * @Description:更新文章
     */
    @RequestMapping("/back/updateArticle")
    @ResponseBody
    public Result updateArticle(ArticleBean articleBean, String ecnryptId) {
        return iArticleService.updateArticle(articleBean, ecnryptId);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/28 14:53
     * @Description:上传翻牌banner文件到七牛云，获取七牛云token
     */
    @RequestMapping("/back/getBannerQnyToken")
    @ResponseBody
    public Result getBannerQnyToken() {
        return iArticleService.getQnyToken("banner", GlobalVar.QNY_ARTICLE_BUCKET);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/28 14:53
     * @Description:上传演示zip文件到七牛云，获取七牛云token
     */
    @RequestMapping("/back/getDemoZipQnyToken")
    @ResponseBody
    public Result getQnyToken() {
        return iArticleService.getQnyToken("demoZip", GlobalVar.QNY_DEMO_BUCKET);
    }

    /**
     * @return
     * @Author: BinZhang
     * @Date: 2018/5/28 18:06
     * @Description:删除上传到七牛云的文章banner和压缩包
     */
    @RequestMapping("/back/delQnyArtFile")
    @ResponseBody
    public Result delQnyArtFile(String key, String type) throws QiniuException {
        return iArticleService.delQnyArtFile(key, type);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/29 17:17
     * @Description:生成下载连接 artId:关联的演示文章id
     * fileName：下载取名
     */
    @RequestMapping("/getDownLoadUrl")
    @ResponseBody
    public Result getDownLoadUrl(String artId, HttpServletRequest request) throws UnsupportedEncodingException {
        return iArticleService.getDownLoadUrl(artId, request);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/30 15:31
     * @Description:更新banner图片地址
     */
    @RequestMapping("/back/updateBannerUrl")
    @ResponseBody
    public Result updateBannerUrl(String artId, String key, String oldKey) {
        return iArticleService.updateBannerUrl(artId, key, oldKey);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/30 15:31
     * @Description:更新演示文件地址 *
     */
    @RequestMapping("/back/updateZipFileUrl")
    @ResponseBody
    public Result updateZipFileUrl(String artId, String key, String oldKey) {
        return iArticleService.updateZipFileUrl(artId, key, oldKey);
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/6/2 16:39
     * @Description:记录文章访问次数
     */
    @RequestMapping(value = "/recordArtCount", method = RequestMethod.POST)
    @ResponseBody
    public void recordArtCount(String artId) {
        iArticleService.recordArtCount(artId);
    }

}
